/*
 * Copyright© 2013-2018 YZTC 
 * Author zhenghl 
 * 本公司保留所有下述内容的权利; 
 * 本内容为保密信息，仅限本公司内部使用; 
 * 非经本公司书面许可，任何人不得外泄或用于其他目的; 
*/
package com.wxmp.core.interceptor;
import java.io.IOException; 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wxmp.core.util.SessionUtil;
/**
 * 该过滤器用户从CAS认证服务器中获取登录用户用户名，并填充必要的Session.
 * @author luck
 *
 */
public class CasForLogoutFilter implements Filter {
	protected final static  Logger logger = LoggerFactory.getLogger(CasForLogoutFilter.class);
	private String casServerLogoutUrl; 
	private String serverNameUrl; 
	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpSession session = ((HttpServletRequest) request).getSession();
		session.removeAttribute(SessionUtil.SESSION_USER);
		session.invalidate();
		logger.info("匹配登录用户失败，重定向到登录页面");
		HttpServletResponse rsp = ((HttpServletResponse) response); 
 		String redirectUrl = casServerLogoutUrl+"?service="+this.serverNameUrl;
 		rsp.sendRedirect(redirectUrl);
	}
	@Override
	public void init(FilterConfig config) throws ServletException {
		this.casServerLogoutUrl = config.getInitParameter("casServerLogoutUrl");
		this.serverNameUrl = config.getInitParameter("serverNameUrl");
	}
}
